package com.app.twth.ui.b2ctrailer;

import android.app.Application;

import androidx.annotation.NonNull;
import androidx.lifecycle.MutableLiveData;

import com.app.twth.app.Constant;
import com.app.twth.base.BaseAndroidViewModel;
import com.app.twth.bean.BaseResponseEntity;
import com.app.twth.bean.LoadingBean;
import com.app.twth.net.JsonCallBack;
import com.app.twth.ui.b2btrailer.oblpn.B2BTrailerLpn;
import com.app.twth.ui.b2ctrailer.submit.B2CTrailerSubmitQr;
import com.lzy.okgo.model.Response;

import java.util.ArrayList;
import java.util.List;


/**
 * ****************************************************************
 * 文件名称: LoginViewModel
 * 文件描述:
 * 注意事项:
 * 版权声明:
 * ****************************************************************
 */
public class B2CTrailerViewModel extends BaseAndroidViewModel<B2CTrailerDataModel> {

    public MutableLiveData<B2CTrailerInitInfo> infoMutableLiveData = new MutableLiveData<>();
    public MutableLiveData<B2CTrailer> b2cTrailerLiveData = new MutableLiveData<>();
    public MutableLiveData<List<B2CTrailer.DetailListDTO>> detailListLiveData = new MutableLiveData<>();
    public MutableLiveData<String> code = new MutableLiveData<>();

    public MutableLiveData<List<B2CTrailer.DetailListDTO>> submitLiveData = new MutableLiveData<>();

    public B2CTrailerViewModel(@NonNull Application application) {
        super(application);
    }

    public void b2cTrailerInitInfo() {
        isLoading.setValue(new LoadingBean(true, Constant.loading));
        errorResultCode.setValue("");
        mModel.b2cTrailerInitInfo(new JsonCallBack<BaseResponseEntity<B2CTrailerInitInfo>>() {
            @Override
            public void onSuccessData(Response<BaseResponseEntity<B2CTrailerInitInfo>> response) {
                isLoading.setValue(new LoadingBean(false, Constant.loading));
                BaseResponseEntity<B2CTrailerInitInfo> baseResponseEntity = response.body();
                if (baseResponseEntity == null) {
                    return;
                }
                if (baseResponseEntity.getCode() == null) {
                    return;
                }
                if (baseResponseEntity.getCode().equals("200")) {
                    infoMutableLiveData.setValue(baseResponseEntity.data);
                    return;
                }
                infoResultMsg = baseResponseEntity.getMessage();
            }

            @Override
            public void onError(String error) {
                isLoading.setValue(new LoadingBean(false, Constant.loading));
                infoResultMsg = error;
                errorResultCode.setValue(infoResultMsg);
            }
        });
    }

    public void b2cTrailerEntryTrailer() {
        isLoading.setValue(new LoadingBean(true, Constant.loading));
        errorResultCode.setValue("");
        mModel.b2cTrailerEntryTrailer(code.getValue(), new JsonCallBack<BaseResponseEntity<B2CTrailer>>() {
            @Override
            public void onSuccessData(Response<BaseResponseEntity<B2CTrailer>> response) {
                isLoading.setValue(new LoadingBean(false, Constant.loading));
                BaseResponseEntity<B2CTrailer> baseResponseEntity = response.body();
                if (baseResponseEntity == null) {
                    return;
                }
                if (baseResponseEntity.getCode() == null) {
                    return;
                }
                if (baseResponseEntity.getCode().equals("200")) {
                    b2cTrailerLiveData.setValue(baseResponseEntity.data);
                    return;
                }
                infoResultMsg = baseResponseEntity.getMessage();
            }

            @Override
            public void onError(String error) {
                isLoading.setValue(new LoadingBean(false, Constant.loading));
                infoResultMsg = error;
                errorResultCode.setValue(infoResultMsg);
            }
        });
    }

    public void b2cTrailerRemove(int detailId) {
        isLoading.setValue(new LoadingBean(true, Constant.load_delete));
        errorResultCode.setValue("");
        mModel.b2cTrailerRemove(detailId, new JsonCallBack<BaseResponseEntity<List<B2CTrailer.DetailListDTO>>>() {
            @Override
            public void onSuccessData(Response<BaseResponseEntity<List<B2CTrailer.DetailListDTO>>> response) {
                isLoading.setValue(new LoadingBean(false, Constant.loading));
                BaseResponseEntity<List<B2CTrailer.DetailListDTO>> baseResponseEntity = response.body();
                if (baseResponseEntity == null) {
                    return;
                }
                if (baseResponseEntity.getCode() == null) {
                    return;
                }
                if (baseResponseEntity.getCode().equals("200")) {

                    List<B2CTrailer.DetailListDTO> detailListDTOList = new ArrayList<>();
                    if (baseResponseEntity.data != null) {
                        detailListDTOList.addAll(baseResponseEntity.data);
                    }

                    for (B2CTrailer.DetailListDTO detailListDTO : detailListLiveData.getValue()) {
                        if (detailListDTO.getId() == null) {
                            detailListDTOList.add(detailListDTO);
                        }
                    }
                    detailListLiveData.setValue(detailListDTOList);
                    return;
                }
                infoResultMsg = baseResponseEntity.getMessage();
            }

            @Override
            public void onError(String error) {
                isLoading.setValue(new LoadingBean(false, Constant.loading));
                infoResultMsg = error;
                errorResultCode.setValue(infoResultMsg);
            }
        });
    }

    public void b2cTrailerCheckObLpn(String obLpn) {
        isLoading.setValue(new LoadingBean(true, Constant.load_submission));
        errorResultCode.setValue("");
        mModel.b2cTrailerCheckObLpn(obLpn, new JsonCallBack<BaseResponseEntity<Object>>() {
            @Override
            public void onSuccessData(Response<BaseResponseEntity<Object>> response) {
                isLoading.setValue(new LoadingBean(false, Constant.loading));
                BaseResponseEntity<Object> baseResponseEntity = response.body();
                if (baseResponseEntity == null) {
                    return;
                }
                if (baseResponseEntity.getCode() == null) {
                    return;
                }
                if (baseResponseEntity.getCode().equals("200")) {
                    if ((detailListLiveData.getValue() == null)) {
                        detailListLiveData.setValue(new ArrayList<>());
                    }
                    B2CTrailer.DetailListDTO detailListDTO = new B2CTrailer.DetailListDTO();
                    detailListDTO.setObLpn(obLpn);
                    detailListLiveData.getValue().add(detailListDTO);

                    detailListLiveData.setValue(detailListLiveData.getValue());
                    return;
                }
                infoResultMsg = baseResponseEntity.getMessage();
            }

            @Override
            public void onError(String error) {
                isLoading.setValue(new LoadingBean(false, Constant.loading));
                infoResultMsg = error;
                errorResultCode.setValue(infoResultMsg);
            }
        });
    }

    public void b2cTrailerSubmit(List<String> obLpns) {
        B2CTrailerSubmitQr b2CTrailerSubmitQr = new B2CTrailerSubmitQr();
        b2CTrailerSubmitQr.setTrailerId(b2cTrailerLiveData.getValue().getId());
        b2CTrailerSubmitQr.setObLpnList(obLpns);
        isLoading.setValue(new LoadingBean(true, Constant.load_submission));
        errorResultCode.setValue("");
        mModel.b2cTrailerSubmit(b2CTrailerSubmitQr, new JsonCallBack<BaseResponseEntity<List<B2CTrailer.DetailListDTO>>>() {
            @Override
            public void onSuccessData(Response<BaseResponseEntity<List<B2CTrailer.DetailListDTO>>> response) {
                isLoading.setValue(new LoadingBean(false, Constant.loading));
                BaseResponseEntity<List<B2CTrailer.DetailListDTO>> baseResponseEntity = response.body();
                if (baseResponseEntity == null) {
                    return;
                }
                if (baseResponseEntity.getCode() == null) {
                    return;
                }
                if (baseResponseEntity.getCode().equals("200")) {
                    submitLiveData.setValue(baseResponseEntity.data);
                    return;
                }
                infoResultMsg = baseResponseEntity.getMessage();
            }

            @Override
            public void onError(String error) {
                isLoading.setValue(new LoadingBean(false, Constant.loading));
                infoResultMsg = error;
                errorResultCode.setValue(infoResultMsg);
            }
        });
    }

}
